package com.xuecheng.learning.service;

import com.xuecheng.framework.domain.course.response.TeachplanMediaPub;
import com.xuecheng.framework.domain.learning.response.GetMediaResult;
import com.xuecheng.framework.domain.learning.response.LearningCode;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.learning.client.CourseSearchClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LearningService {
    //注入远程调用服务
    @Autowired
    CourseSearchClient courseSearchClient;



    /**
     * 获取课程学习地址(视频播放地址)
     * @param courseId  课程id  *(校验学生是否有资格可看)
     * @param teachplanId    课程计划id  *(视频地址 小海豚tearchplan_media_pub)
     * @return
     */
    public GetMediaResult getMedia(String courseId, String teachplanId) {
        //第一件事:校验学生的学生权限


        //第二件事:远程调用搜索服务,查询课程计划所对应的课程媒资信息
        //远程调用搜索服务查询课程计划所对应的课程媒资信息
        TeachplanMediaPub teachplanMediaPub = courseSearchClient.getmedia(teachplanId);
        if(teachplanMediaPub == null || StringUtils.isEmpty(teachplanMediaPub.getMediaUrl())){
            //获取学习地址错误
            ExceptionCast.cast(LearningCode.LEARNING_GETMEDIA_ERROR);
        }
        return new GetMediaResult(CommonCode.SUCCESS,teachplanMediaPub.getMediaUrl());
    }

}
